package sw6.visualschedule.extendedViews;

import java.util.List;

import sw6.lib.Settings;
import sw6.visualschedule.R;
import sw6.visualschedule.entities.ScheduleActivity;
import sw6.visualschedule.entities.Step;
import sw6.visualschedule.persistence.StepRepo;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class StepViewAdapter extends BaseAdapter {

	private final static int FINISH_STEP_ID = -1;
	
	private final transient Context mContext;
	private final transient List<Step> mSteps;
	private final transient OnClickListener mFinishClickListener;
	private final transient OnClickListener mStepDoneClickListener;
	
	private final int mMaxWidth;
	private final int mMaxHeight;
	
	/**
	 * Constructs the stepview adapter.
	 * @param context the context to run in.
	 * @param activity the activity related.
	 * @param onFinishClickListener a callback for onFinish events.
	 * @param stepDoneClickListener a callback for Step Done events.
	 * @param maxWidth the maximum width of images in the step view.
	 * @param maxHeight the maximum height of images in the step view.
	 */
    public StepViewAdapter(final Context context, final ScheduleActivity activity, final OnClickListener onFinishClickListener, final OnClickListener stepDoneClickListener, final int maxWidth, final int maxHeight) {
        super();
    	this.mContext = context;
    	this.mSteps  = new StepRepo(mContext).getOrderedSteps(activity.getId());
    	final Step step = new Step(activity, mContext.getResources().getString(R.string.stepview_finish_activity_title));
    	this.mSteps.add(step);
    	this.mFinishClickListener = onFinishClickListener;
    	this.mStepDoneClickListener = stepDoneClickListener;
    	
    	mMaxWidth = maxWidth;
    	mMaxHeight = maxHeight;
    }
    
	@Override
	public int getCount() {
		return mSteps.size();
	}

	@Override
	public Step getItem(final int position) {
		return mSteps.get(position);
	}

	@Override
	public long getItemId(final int position) {
	    long result = FINISH_STEP_ID;
	    
		if (position < mSteps.size() - 1) {
			result = mSteps.get(position).getId();
		}
		
		return result;
	}
	
	@Override
	public View getView(final int position, final View convertView, final ViewGroup parent) {
		final Step step = getItem(position);
		final StepView stepView = new StepView(mContext);
		
		if (Settings.getUserProfile(mContext).canRead()) {
			stepView.getText().setVisibility(View.VISIBLE);
			stepView.getText().setText(step.getTitle());
		}
		
		if (getItemId(position) == FINISH_STEP_ID) {
			stepView.getImage().setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.finish));
			stepView.getDoneButton().setOnClickListener(mFinishClickListener);
			return stepView;
		}
		
		stepView.getImage().setImageBitmap(step.getImageBitmapThumbnail(mContext, mMaxWidth, mMaxHeight));
		
		stepView.getImage().setOnClickListener(step);
		stepView.getDoneButton().setOnClickListener(mStepDoneClickListener);
		
		return stepView;
	}
}